using System;
using System.ComponentModel;
using System.Web.UI;

namespace PS.WebParts.Rollup
{
    public class ExecutionStats
    {
        DateTime queryBegin;
        DateTime? postProcessingBegin = null;
        DateTime queryEnd;

        public DateTime QueryBegin
        {
            get { return queryBegin; }
        }

        public DateTime PostProcessingBegin
        {
            get { return postProcessingBegin.HasValue ? postProcessingBegin.Value : DateTime.MinValue; }
        }

        public DateTime QueryEnd
        {
            get { return queryEnd; }
        }

        public TimeSpan ElapsedQueryTime
        {
            get
            {
                return queryEnd - queryBegin; 
            }
        }

        public TimeSpan ElapsedPostProcessingTime
        {
            get
            {
                return postProcessingBegin.HasValue ? queryEnd - postProcessingBegin.Value : TimeSpan.Zero;
            }
        }

        internal void SetQueryBegin()
        {
            queryBegin = DateTime.Now;
        }

        internal void SetQueryEnd()
        {
            queryEnd = DateTime.Now;
        }

        public void SetPostProcessingBegin()
        {
            postProcessingBegin = DateTime.Now;
        }
    }

    public abstract class ATabularDataSource : DataSourceControl
    {
        string rootName = "dsQueryResponse";
        string rowsName = "Rows";

        ATabularDataSourceView _dsv;

        abstract protected ATabularDataSourceView CreateViewInstance();

        protected internal ATabularDataSourceView GetView()
        {
            if (_dsv == null)
            {
                //#if !TESTS                    
                //                    if (IsFPDesignMode)
                //                        _dsv = new RollUpDesignDataSourceView(base.Context, this, string.Empty);
                //                    else
                //#endif
                //                    {
                _dsv = CreateViewInstance();
                //}
            }
            return _dsv;
        }

        [Obsolete]
        public string TopLevelSite
        {
            set
            {
            }
        }

        internal string ProcessingMessages
        {
            get
            {
                return GetView().ProcessingMessages;
            }
        }

        [DefaultValue("dsQueryResponse")]
        public string RootName
        {
            get
            {
                return rootName;
            }
            set
            {
                rootName = value;
            }
        }
        
        [DefaultValue("Rows")]
        public string RowsName
        {
            get
            {
                return rowsName;
            }
            set
            {
                rowsName = value;
            }
        }

        public ExecutionStats GetStats()
        {
            return _dsv.Stats;
        }
    }
}
